<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    function myNew(constr){
        // 1- 创建一个对象obj
        var obj = {};
        // 2- 将obj的隐式原型指向到构造函数（constr)下的显示原型
        obj.__proto__ = constr.prototype;
        // 3- 将构造函数constr中的this指向为obj,并执行构造函数
        const result = constr.apply(obj,Array.from(arguments).slice(1));
        // 4- 返回值
        if(typeof result === "object" || typeof result === "function"){
            return result;
        }
        return obj;
    }
    function Box(userName,age){
        this.userName = userName;
        this.age = age;
        return 1
    }
    // const obj = new Box("zhangsan",12);
    // console.log(obj);

    const obj = myNew(Box,"zhangsan",12);
    console.log(obj);// {a:1}
    // console.log(obj.userName);// zhangsan
    // console.log(obj.age);// 12
    // console.log(obj.__proto__ === Box.prototype);// true
</script>
</html>